﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using common;
using dao;
namespace GTVAccountingSystem.view.form
{
    public partial class frmForgetPassword : Form
    {
        UsersDAO usersDAO = new UsersDAO();
        String error = "";
        private String userNameText = "";
        private String emailText = "";
        frmLogin login;
        public frmForgetPassword(frmLogin login)
        {
            this.login = login;
            InitializeComponent();
            disableLableError();
            setLanguage();
        }
        /* Author : Jenny
         * Date : 10/4/2012
         * function setLanguage
         * set language
         * no return
         */
        public void setLanguage()
        {
            lblUsername.Text = Constant.RM.GetString("lblUsername", Constant.CULTURE) + " : ";
            lblEmail.Text = Constant.RM.GetString("lblEmail", Constant.CULTURE) + " : ";
            btnSendForManager.Text = Constant.TAB + Constant.RM.GetString("btnSendForManager", Constant.CULTURE);
            this.Text = Constant.RM.GetString("frmForgetPassword", Constant.CULTURE);
        }
        /* Author : Jenny
         * Date : 10/4/2012
         * function btnSendForManager_Click
         * notice manager about the forget password
         * no return
         */
        private void btnSendForManager_Click(object sender, EventArgs e)
        {
            validateData();
            if (error.Equals(""))
            {
                int result = usersDAO.forgetPassword(userNameText, emailText);
                if (result == 0)
                {
                    if (MessageBox.Show("Do you really want to send this???", "Forget Password", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        usersDAO.sendForgetPassword(Constant.USER_LOGIN.Id, Constant.DR_ACTIONS_VARIABLE["varForgetPassword"].ToString(), "");
                        login.Visible = true;
                        this.Close();
                    }
                }
                else
                {
                    if (result == -3)
                        MessageBox.Show("your account has been locked !!!");
                    else if (result == -2)
                    {
                        MessageBox.Show("your username is not correct !!!");
                    }
                    else if (result == -1)
                    {
                        if (Constant.COUNT_FORGET_PASSWORD_FAIL == 3)
                            MessageBox.Show("You input wrong email 3 times, your account will be locked!!!");
                        else
                            MessageBox.Show("You input wrong email " + Constant.COUNT_FORGET_PASSWORD_FAIL + " times");
                    }
                }
            }
            else
            {
                MessageBox.Show(error);
            }
        }
       /* Author : Jenny
        * Date : 10/4/2012
        * function validateData
        * validate data
        * no return
        */
        public void validateData()
        {
            error = "";
            userNameText = txtUsername.Text.Trim();
            emailText = txtEmail.Text.Trim();
            disableLableError();
            if (userNameText.Equals(""))
            {
                txtUsername.BackColor = Color.MistyRose;
                error += "Input username\n";
            }
            if(emailText.Equals(""))
            {
                txtEmail.BackColor = Color.MistyRose;
                error += "Input Email";
            }
        }
        /* Author : Jenny
         * Date : 10/4/2012
         * function disableLableError
         * hide the error lable when start
         * no return
         */
        public void disableLableError()
        {
            txtUsername.BackColor = SystemColors.Window;
            txtEmail.BackColor = SystemColors.Window;
        }
    }
}
